home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / graphics / hilbert.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  1KB  |  103 lines

  1. #include <LEDA/window.h>
  2.  
  3. window W;
  4.  
  5. double x, y, dx, dy;
  6.  
  7. void A(int);
  8. void B(int);
  9. void C(int);
  10. void D(int);
  11.  
  12.  
  13. void plot(double new_x, double new_y)
  14. { W.draw_segment(x,y,new_x,new_y);
  15.   x = new_x;
  16.   y = new_y;
  17.  }
  18.  
  19.  
  20. void A(int i)
  21.   if (i > 0)
  22.   { D(i-1); plot(x-dx,y);
  23.     A(i-1); plot(x,y-dy);
  24.     A(i-1); plot(x+dx,y); 
  25.     B(i-1);
  26.    }
  27.  }
  28.  
  29. void B(int i)
  30.   if (i > 0)
  31.   { C(i-1); plot(x,y+dy);
  32.     B(i-1); plot(x+dx,y);
  33.     B(i-1); plot(x,y-dy); 
  34.     A(i-1);
  35.    }
  36.  }
  37.  
  38.  
  39.  
  40. void C(int i)
  41.   if (i > 0)
  42.   { B(i-1); plot(x+dx,y);
  43.     C(i-1); plot(x,y+dy);
  44.     C(i-1); plot(x-dx,y); 
  45.     D(i-1);
  46.    }
  47.  }
  48.  
  49. void D(int i)
  50.   if (i > 0)
  51.   { A(i-1); plot(x,y-dy);
  52.     D(i-1); plot(x-dx,y);
  53.     D(i-1); plot(x,y+dy); 
  54.     C(i-1);
  55.    }
  56.  }
  57.  
  58.  
  59. int n = 5;
  60.  
  61. void hilbert()
  62. {
  63.    double lx = W.xmax() - W.xmin();
  64.    double ly = W.ymax() - W.ymin();
  65.  
  66.    double x0 = W.xmin() + 0.98*lx;
  67.    double y0 = W.ymin() + 0.98*ly;
  68.  
  69.    dx = 0.96 * lx/(1 << n);
  70.    dy = 0.96 * ly/(1 << n);
  71.  
  72.    x = x0;
  73.    y = y0;
  74.  
  75.    A(n);
  76.  
  77.    W.draw_segment(x0,y0,x0+dx,y0);
  78.    W.draw_segment(x0+dx,y0,x0+dx,y);
  79.    W.draw_segment(x0+dx,y,x,y);
  80.  
  81.   }
  82.  
  83. main()
  84. {   
  85.  panel P("hilbert curve");
  86.  P.int_item("n = ",n,1,10);
  87.  
  88.  W.set_redraw(hilbert);
  89.  
  90.  for(;;)
  91.  { P.open();
  92.    W.clear();
  93.    hilbert();
  94.    W.read_mouse();
  95.   }
  96.  
  97.   return 0;
  98. }
  99.